<?
session_start();
require_once("config.php");
require_once("classes/index.class.php");


// adding twitter API
if (isset($_REQUEST['social']))
	include "fb_test/test.php";

$user_id = $facebook->getUser();
$tokenid  = $facebook->getAccessToken();

$attachment = array('access_token' =>$tokenid,'scope' =>  'user_status,publish_stream,user_photos,photo_upload');

if(isset($_REQUEST['submit'])) {
	if ($_REQUEST['msg'] != "")	$message = $_REQUEST['msg']; else $message = '';
	$attachment['message'] = $message;
	$objUser = new User($user_id);
	if (isset($_FILES['source']['name']) && !empty($_FILES['source']['name']))
	{
		$facebook->setFileUploadSupport(true);	
		$facebook->getLoginUrl(array('scope' => 'user_status,publish_stream,user_photos,photo_upload'));
		$fname = $_FILES['source']['tmp_name'];
		$attachment['source'] = '@'.realpath($fname);
		$result = $facebook->api('/me/photos', 'POST', $attachment);	  			
	}
	else{
		$result = $facebook->api("/$user_id/feed/",'post',$attachment);
	}
	// post to twitter
	if(isset($_SESSION['ot'])) {
		require_once('twitter/lib/secret.php');
		require_once('twitter/lib/EpiCurl.php');
		require_once('twitter/lib/EpiOAuth.php');
		require_once('twitter/lib/EpiTwitter.php');
		require_once('fb_test/AppInfo.php');


		$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
		$twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']);
		if (isset($_FILES['source']['name']) && !empty($_FILES['source']['name']))
		{ // if there is an image
			require_once('twitter/tmhOAuth.php');
			require_once('twitter/tmhUtilities.php');
			$tmhOAuth = new tmhOAuth(array(
			  'consumer_key'    => $consumer_key, 
			  'consumer_secret' => $consumer_secret,
			  'user_token'      => $_SESSION['ot'],
			  'user_secret'     => $_SESSION['ots']
			));
			$image = "@{$_FILES['source']['tmp_name']};type={$_FILES['source']['type']};filename={$_FILES['source']['name']}";
 			$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
			  array(
				'media[]'  => "@{$image}",
				'status'   => $message
				),true, // use auth
				true  // multipart
				);
				//var_dump($code);
		} else { // text only
			$update_status = $twitterObj->post_statusesUpdate(array('status' => $message));
			$temp = $update_status->response;
		}
	}
	
}
// Get FaceBook Feed
$feedlist = $facebook->api("/$user_id/statuses?limit=$feedLimit"); $i=0;$arFeed = array();$arPersonal = array();
foreach($feedlist['data'] as $key=>$value)
{
	if(isset($feedlist['data'][$key]['message']) && !empty($feedlist['data'][$key]['message']))
	{
		$id = $feedlist['data'][$key]['from']['id'];unset($arPersonal);
		//$arPersonal = $facebook->api("/$id/",'get',array('access_token' =>$tokenid));
		$kid = $feedlist['data'][$key]['id'];
		$arFeed[$kid]= array('message'=>$feedlist['data'][$key]['message'],
					  'time' => date("m/d/Y, H:i:s",strtotime($feedlist['data'][$key]['updated_time'])).' EST',
					  'name' => $feedlist['data'][$key]['from']['name'],
					  'link' => 'http://facebook.com/'. $id,
					  'profilePic' => 'http://graph.facebook.com/' . $id . '/picture')
					  ;
		
		$i++;
	}
	if($i == $feedLimit) break;
	if(isset($feedlist['data'][$key]['comments']['data']))
	{
		$j=0;		
		foreach($feedlist['data'][$key]['comments']['data'] as $comkey=>$comval)
		{
			if(isset($feedlist['data'][$key]['comments']['data'][$comkey]['message']))
			{
				unset($arPersonal);
				$personid = $feedlist['data'][$key]['comments']['data'][$comkey]['from']['id'];				

				//$arPersonal = $facebook->api("/$personid/",'get',array('access_token' =>$tokenid));			
	
				$arFeed[$kid]['comments'][]= array('message'=>$feedlist['data'][$key]['comments']['data'][$comkey]['message'],
					  'time' => date("m/d/Y, H:i:s",strtotime($feedlist['data'][$key]['comments']['data'][$comkey]['created_time'])).' EST',
					  'name' => $feedlist['data'][$key]['comments']['data'][$comkey]['from']['name'],
					  'link' => 'http://facebook.com/'.$personid,
					  'profilePic' => 'http://graph.facebook.com/' . $personid . '/picture')
					  ;
				$i++;$j++;
			}
			if($j == $commentfeed) break;			
			if($i == $feedLimit) break;
		}
	}
}

// get Twitter info
if(! isset($username)) {
require_once('twitter/lib/secret.php');
require_once('twitter/lib/EpiCurl.php');
require_once('twitter/lib/EpiOAuth.php');
require_once('twitter/lib/EpiTwitter.php');
require_once('fb_test/AppInfo.php');


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);



if (isset($_GET['oauth_token']) && $_GET['oauth_token'] != '') {
	$twitterObj->setToken($_GET['oauth_token']);
	$token = $twitterObj->getAccessToken();
	$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);	  	
	$_SESSION['ot'] = $token->oauth_token;
	$_SESSION['ots'] = $token->oauth_token_secret;
}

if(isset($_SESSION['ot'])) {
	$twitterObj->setToken($_SESSION['ot'], $_SESSION['ots']);
	$twitterInfo = $twitterObj->get_accountVerify_credentials();
	$username = $twitterInfo->screen_name;

	if ($username != '') {
		$bLoggedinTwitter = true;
	} else {
		$bLoggedinTwitter = false;
	}
} else {
	$bLoggedinTwitter = false;
} 
	

 $args = array('grant_type' => 'client_credentials', 

   'client_id' => AppInfo::appID(), 

   'client_secret' =>  AppInfo::appSecret());

 $ch = curl_init(); 

 $url = 'https://graph.facebook.com/oauth/access_token'; 

 curl_setopt($ch, CURLOPT_URL, $url); 

 curl_setopt($ch, CURLOPT_HEADER, false); 

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

 curl_setopt($ch, CURLOPT_POST, true); 

 curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 

 $result = curl_exec($ch); 

 if ($result===false)

	//echo 'false';

 curl_close($ch);
}

require_once("home.html");
?>
